/*
 * UIService.fx
 *
 * Created on Oct 4, 2009, 6:04:20 PM
 */

package dawnland02.ui;

import javafx.scene.paint.Color;
import javafx.util.Math;

import dawnland02.data.map.model.Matrix;
import dawnland02.data.map.model.MatrixDirection;
import dawnland02.service.GeneralService;

/**
 * @author user
 */

public class UIService {
    package var uiMatrix : UIMatrix;

    def UIHEXAGON_VERTICAL_EDGE = 20.0; //x
    def UIHEXAGON_PREF_WIDTH = UIHEXAGON_VERTICAL_EDGE *  Math.sqrt(3.0); //x*sqrt3
    //def UIHEXAGON_PREF_WIDTH = UIHEXAGON_VERTICAL_EDGE *  1.7; //x*sqrt3
    def UIHEXAGON_PREF_HEIGHT = 2 * UIHEXAGON_VERTICAL_EDGE; //2*x

    def colors = [Color.web("#094D02"), Color.web("#3D2B01"), Color.web("#AF9F51"), Color.web("#499D91")]; //grass, wood, stone, iron

    init{
      println("init of UIService - this should happen only once.");
    }
    
    public function getUIHexagonPrefWidth () : Number {
      return UIHEXAGON_PREF_WIDTH;
    }

    public function getXStep() : Number {
        return getUIHexagonPrefWidth() / 2;
    }

    public function getUIHexagonPrefHeight () : Number {
      return UIHEXAGON_PREF_HEIGHT;
    }

    public function getYStep() : Number {
        return getUIHexagonPrefHeight() / 4;
    }

    public function getColor(index: Integer): Color {
      return colors[index];
    }

    public function getAngle(matrixDirection: MatrixDirection): Number {
        if (matrixDirection == MatrixDirection.UP){
            return 0;
        } else if (matrixDirection == MatrixDirection.RIGHT_UP){
            return 30;
        } else if (matrixDirection == MatrixDirection.RIGHT){
            return 90;
        } else if (matrixDirection == MatrixDirection.RIGHT_DOWN){
            return 150;
        } else if (matrixDirection == MatrixDirection.LEFT_DOWN){
            return -150;
        } else if (matrixDirection == MatrixDirection.LEFT){
            return -90;
        } else if (matrixDirection == MatrixDirection.LEFT_UP){
            return -30;
        }
        return 0.0;
    }


    /*
     Factory functions
    */

    public function getUIMatrix() : UIMatrix {
        if(uiMatrix == null){
            uiMatrix = UIMatrix{
                matrix: GeneralService.getMatrix();
                uiService : this;
            };
        }
        uiMatrix;
    }

    public function createUIHexagonsFromMatrix(matrix: Matrix) : UIHexagon[] {
       var uiHexagons : UIHexagon[];
       var uiHexagon : UIHexagon;
       for(matrixCell in matrix.getNodesAsArray()){
        uiHexagon = UIHexagon {
            matrixCell: matrixCell;
            uiService: this;            
            }
        insert uiHexagon into uiHexagons;
        }
       uiHexagons;
    }

    public function createUIEntity(uiHexagon : UIHexagon ) : UIEntity{
        UIEntity {
            entity : GeneralService.createEntity(uiHexagon.matrixCell.getX(), uiHexagon.matrixCell.getY());
            uiHexagon : uiHexagon;
            uiService : this;
        }
    }

}
